// pages/ptwords/ptwords.js
const app = getApp()
var base_url = app.globalData.url
var clientHeight = app.globalData.clientHeight
// var appId = "wxa8d323dd303e43ec";
// var secret = '53eeb275d0bf39a9dbc8370601a95766';
var appId = "wx832b5cf39f1ba743";
var secret = '352b46c1077186a3dc391f34c7da1fad';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    develop:"none",
    tabBar:'33%',
    home: {
      "current": 0,
      "pagePath": "/pages/home/ptwords/ptwords",
      "iconPath": "/images/icons/icona.png",
      "text": "习字"
    },
    photo: {
      "current": 0,
      "pagePath": "/pages/photo/goTest/goTest",
      "iconPath": "/images/icons/icon1.png",
      "text": "评测"
    },
    mine: {
      "current": 0,
      "pagePath": "/pages/my/visitor/mine/mine",
      "iconPath": "/images/icons/icon4_1.png",
      "text": "我的"
    },
    find: {
      "current": 0,
      "pagePath": "/pages/find/find",
      "iconPath": "/images/icons/icon3.png",
      "text": "班级"
    },

    loginView: "none",
    displayFindtabbar: "none",
    displayPhototabbar: "block",
    height: clientHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var code = ''
    // var openid = ''
    // var access_token = ''
    // var unionid = ''
    app.userType = '1'
    var that = this



    // 获取地理位置
    wx.getLocation({
      success: function (res) {
        console.log("locationsuccess:", res)
      },
      fail: function (res) {
        console.log("locationfail:", res)
      }
    })


    //获取用户信息
    wx.getUserInfo({
      success: function(res) {
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          loginView: "none"
        })
      },
      fail:function(res){
        console.log("getUserInfoFail:",res)
        that.setData({
          mine: {
            "current": 0,
            "pagePath": "/pages/my/noauth/noauth",
            "iconPath": "/images/icons/icon4_1.png",
            "text": "我的"
          },
          loginView: "block"
        })
      }
    })
    
    //判断用户身份
    if(app.globalData.loginType == 1){
      wx.getStorage({
        key: 'auth',
        success: function (res) {
          console.log(res.data)
          if (res['data'] == 1) {
            that.setData({
              displayFindtabbar: "block",
              displayPhototabbar: "none",
              mine: {
                "current": 0,
                "pagePath": "/pages/my/teacher/mine/mine",
                "iconPath": "/images/icons/icon4_1.png",
                "text": "我的"
              },
              // tabBar: "25%",
            })
          }else{
            that.setData({
              displayFindtabbar: "none",
              displayPhototabbar: "block",
              // tabBar: "25%",
            })
          }
        }
      }) 
    }else{

    wx.login({
      success: function (res) {
        var code = ''
        code = res.code
        //获取openid
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            appid: 'wx832b5cf39f1ba743',
            secret: '352b46c1077186a3dc391f34c7da1fad',
            js_code: code,
            grant_type: 'authorization_code'
          },
          success: function (res) {
            var session_key = ''
            var encryptedData = ''
            var iv = ''
            session_key = res.data.session_key
            console.log(res)
            wx.getUserInfo({
              success: function (res) {
                app.globalData.loginType = 1//修改用户状态
                encryptedData = res.encryptedData
                iv = res.iv
                wx.request({
                  url: base_url + '/wxloginxcx/',
                  method: 'POST',
                  data: {
                    session_key: session_key,
                    encryptedData: encryptedData,
                    iv: iv
                  },
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function (res) {
                    console.log(res.data['flag'])
                    if (res.data['flag'] == true) {
                      console.log(res.data['auth'])
                      if (res.data['auth'] == '1') {//auth:1权限为教师
                        var teacher_id = res.data['teacher_id']
                        that.setData({
                          mine: {
                            "current": 0,
                            "pagePath": "/pages/my/teacher/mine/mine",
                            "iconPath": "/images/icons/icon4_1.png",
                            "text": "我的"
                          },
                          displayFindtabbar: "block",
                          displayPhototabbar: "none",
                          // tabBar: "25%",
                        })
                        wx.setStorage({
                          key: 'auth',
                          data: '1',
                          success: function () {
                          }
                        })
                        wx.setStorage({
                          key: 'teacher_id',
                          data: teacher_id,//存入教师id
                          success: function () {
                            wx.request({
                              url: base_url + '/setAvatarUrl/',//保存头像
                              method: 'POST',
                              data: {
                                type: 1,
                                teacher_id: teacher_id,
                                avatarUrl: that.data.avatarUrl
                              },
                              header: {
                                "Content-Type": "application/x-www-form-urlencoded"
                              },
                              success: function (res) { }
                            })
                          }
                        })
                      } else if (res.data['auth'] == '0') {//auth:0权限为学生
                        var student_id = res.data['student_id']
                        wx.setStorage({
                          key: 'student_id',
                          data: student_id,
                          success: function () {
                            wx.request({
                              url: base_url + '/setAvatarUrl/',
                              method: 'POST',
                              data: {
                                type: 0,
                                student_id: student_id,
                                avatarUrl: that.data.avatarUrl
                              },
                              header: {
                                "Content-Type": "application/x-www-form-urlencoded"
                              },
                              success: function (res) {
                                that.setData({
                                  mine: {
                                    "current": 0,
                                    "pagePath": "/pages/my/visitor/mine/mine",
                                    "iconPath": "/images/icons/icon4_1.png",
                                    "text": "我的"
                                  },
                                })
                              }
                            })
                          }
                        })
                        that.setData({
                          // tabBar: "25%",
                        })
                        wx.setStorage({
                          key: 'auth',
                          data: '0',
                          success: function () {
                            console.log(res)
                          }
                        })
                      } else {//新用户
                        var student_id = res.data['student_id']
                        wx.setStorage({
                          key: 'student_id',
                          data: student_id,
                          success: function () {
                            that.setData({
                              mine: {
                                "current": 0,
                                "pagePath": "/pages/my/visitor/mine/mine",
                                "iconPath": "/images/icons/icon4_1.png",
                                "text": "我的"
                              },
                            })
                            console.log()
                          }
                        })
                      }
                    }
                  }
                })

              },
              fail: function (res) {//登陆失败
                that.setData({
                  mine: {
                    "current": 0,
                    "pagePath": "/pages/my/noauth/noauth",
                    "iconPath": "/images/icons/icon4_1.png",
                    "text": "我的"
                  },
                  loginView: "block"
                })
              }
            })

          }
        })

      }
    })
  }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var that = this;
    //获取首页数据
    wx.request({
      url: base_url + '/getHomeData/',
      method: 'POST',
      data: {
        type: '1'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          that.setData({
            books_content_datas: res.data['datas'],
            imgUrls: res.data['imgUrls']
          })
        }
      }
    })



  },
  //跳转到评测
  toPhoto:function(){
    wx.navigateTo({
      url: '../../photo/goTest/goTest',
      success: function (res) {
        console.log(res)
      },
      fail: function () { }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.loginType = 1
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //搜索文字
  searchWord: function() {
    var that = this
    wx.request({
      url: base_url +'/searchWord/',
      method: 'POST',
      data: {
        word: this.data.searchWord,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        that.setData({
          inputValue: ""
        })
        console.log(res.data['words_lst'])
        if (res.data['flag'] == true) {
          
          wx.setStorage({
            key: 'searchwordDatas',
            data: res.data['words_lst'],
            success: function(res) {

            }
          })

        }
        if (res.data['flag'] == true) {
          wx.navigateTo({
            url: '../searchword/searchword',
            success: function(res) {
              console.log(res)
            },
            fail: function() {}
          })

        } else {
          wx.showToast({
            title: '输入文字不存在',
            icon: 'loading',
            duration: 2000
          })
        }
      }
    })
  },
  //搜索框输入
  searchInput: function(e) {
    this.setData({
      searchWord: e.detail.value
    })
  },
  //跳转到课程
  gotoCourse: function(e) {
    var get_bookName = e.currentTarget.dataset.name
    wx.request({
      url: base_url +'/getCourseDatas/',
      method: 'POST',
      data: {
        get_bookName: get_bookName,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res.data['flag'])
        if (res.data['flag'] == true) {
          wx.setStorage({
            key: 'shizi_lst',
            data: res.data['shizi_lst'],
            success: function() {
              console.log(res)
            }
          })
          wx.setStorage({
            key: 'kewen_lst',
            data: res.data['kewen_lst'],
            success: function () {
              console.log(res)
            }
          })
          wx.navigateTo({
            url: '../course/course',
            success: function(res) {
              console.log(res)
            },
            fail: function() {}
          })
        } else {

        }
      }
    })
  },


//跳转到扫一扫
  scanTo:function(){
    wx.navigateTo({
      url: '../scan/scan',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  //用户授权
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      
      // that.setData({
      //   loginView:"none"
      // })
      that.onLoad()

    } else {
      that.setData({
        loginView: "none"
      })
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {

        }
      })
    }
  },

//跳转正在开发
  gotoBi:function(){
    var that = this
    that.setData({
      develop:"block"
    })
    // wx.navigateTo({
    //   // url: '../../my/visitor/selectClass/selectClass',
    //   url: '../../photo/allResult/allResult',
    //   success: function (res) {
    //     console.log(res)
    //   },
    //   fail: function () {
    //   }
    // })
  },
  //点击正在开发取消
  nok:function(){
    var that = this
    that.setData({
      develop: "none"
    })
  },
  

})